package com.example.RuanJianKeShe.Service.teacher;

import com.alibaba.fastjson.JSON;
import com.example.RuanJianKeShe.Bean.SC;
import com.example.RuanJianKeShe.Bean.Student;
import com.example.RuanJianKeShe.Bean.TC;
import com.example.RuanJianKeShe.Dao.impl.StuCourseDaoImpl;
import com.example.RuanJianKeShe.Dao.impl.TeaCourseDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@WebServlet("/getAllStuByCno")
public class getAllStuByCno extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("application/json;charset=utf-8");
        String cno = req.getParameter("cno");
        TeaCourseDaoImpl teaCourseDao = new TeaCourseDaoImpl();
        List<TC> allTC = teaCourseDao.getAllTC();
        StuCourseDaoImpl stuCourseDao = new StuCourseDaoImpl();
        List<Student> students = stuCourseDao.getStudentBySC(Integer.parseInt(cno));
        List<SC> allSC = stuCourseDao.getAllSC();
        List<HashMap<String,Object>> snoAndGradeList = new ArrayList<>();
        for (int i=0;i<students.size();i++){
            HashMap<String,Object> snoAndGrade = new HashMap<>();
            for (int j=0;j<allSC.size();j++){
                if (allSC.get(j).getSno()==students.get(i).getSno()&&allSC.get(j).getCno()==Integer.parseInt(cno)){
                    snoAndGrade.put("sno",students.get(i).getSno());
                    snoAndGrade.put("sname",students.get(i).getSname());
                    snoAndGrade.put("grade",allSC.get(j).getGrade());
                    snoAndGradeList.add(i,snoAndGrade);
                }

            }
        }
        HashMap<String,Object> map = new HashMap<>();
        map.put("snoAndGradeList",snoAndGradeList);
        String jsonString = JSON.toJSONString(map);
        resp.getWriter().write(jsonString);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
